package com.sunday.volunteer.admin.adapter.group;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.ApplyMember;
import com.sunday.volunteer.admin.utils.CircleImageView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/1.
 * 游玩需求
 */
public class GroupApplyRecycleAdapter extends RecyclerView.Adapter {

    private Context context;
    private int width;
    private List<ApplyMember> applyMemberList;

    public GroupApplyRecycleAdapter(Context context,List<ApplyMember> applyMemberList) {
        this.context = context;
        width = DeviceUtils.getDisplay(context).widthPixels;
        this.applyMemberList = applyMemberList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.group_apply_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holder1 = (ViewHolder) holder;
        final ApplyMember applyMember = applyMemberList.get(position);
        int status = applyMember.getStatus();//1---未审核,2---通过
        if (!TextUtils.isEmpty(applyMember.getMemberLogo())){
            Picasso.with(context)
                    .load(applyMember.getMemberLogo())
                    .resize(220,220)
                    .placeholder(R.mipmap.person_default)
                    .error(R.mipmap.person_default)
                    .into(holder1.imgHead);
        }
        holder1.tvName.setText(applyMember.getMemberName());
        holder1.tvType.setText(applyMember.getGroupType());
        if (status==2){
            holder1.tvRefuse.setVisibility(View.INVISIBLE);
            holder1.tvAgree.setText("已通过");
        }else if (status==1){
            holder1.tvAgree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.itemClick(v, position,applyMember.getId());
                    }
                }
            });
            holder1.tvRefuse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.itemClick(v, position,applyMember.getId());
                    }
                }
            });
        }else {
            holder1.tvRefuse.setVisibility(View.INVISIBLE);
            holder1.tvAgree.setText("已拒绝");
            holder1.tvAgree.setTextColor(Color.parseColor("#666666"));
            holder1.tvAgree.setBackgroundResource(R.drawable.apply_bg2);
        }
        holder1.lMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.itemClick(v, position,applyMember.getId());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return applyMemberList.size();
    }

    public interface ItemClickListener {
        void itemClick(View v, int position,long id);
    }

    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.img_head)
        CircleImageView imgHead;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_type)
        TextView tvType;
        @Bind(R.id.tv_refuse)
        TextView tvRefuse;
        @Bind(R.id.tv_agree)
        TextView tvAgree;
        @Bind(R.id.l_main)
        RelativeLayout lMain;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
